package org.micode.demo.web.controller;

import lombok.extern.slf4j.Slf4j;
import org.micode.common.annotation.PermissionsFlag;
import org.micode.common.domain.ApiResult;
import org.micode.common.domain.DataPage;
import org.micode.common.domain.vo.KeyVO;
import org.micode.common.helper.I18nMsgHelper;
import org.micode.demo.db.domain.dto.SysDictionaryDTO;
import org.micode.demo.db.domain.query.SysDictionaryQuery;
import org.micode.demo.db.vo.IdNameVO;
import org.micode.demo.web.base.BaseWebAdminController;
import org.micode.demo.web.service.SysDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 字典配置信息管理
 *
 * @author: wutianbin
 * @version: v1.0.27
 * @CreateDate: 2021-09-16
 */
@RestController
@RequestMapping("/web-admin/sys/dictionary")
@Slf4j
public class SysDictionaryController
        extends BaseWebAdminController {

    @Autowired
    private SysDictionaryService sysDictionaryService;

    /**
     * 分页查询字典配置信息列表数据
     *
     * @param dataQuery 查询条件，如果条件中不包含page对象，则系统会指定一个默认的page对象。
     * @return 返回查询结果记录集
     */
    @PostMapping("/list")
    @PermissionsFlag("sys:dictionary:list")
    public ApiResult<DataPage<SysDictionaryDTO>> list(
            @RequestBody SysDictionaryQuery dataQuery) {
        log.debug(I18nMsgHelper.get("api.call.sysDictionary.list") + " == {}", dataQuery);
        return listForQuery(sysDictionaryService::list, dataQuery);
    }

    /**
     * 按主键查询字典配置信息详细信息
     *
     * @param id 要查询的主键值, 例如：{"key": "1"}
     * @return 主键对应的对象
     */
    @PostMapping("/info")
    @PermissionsFlag("sys:dictionary:info")
    public ApiResult<SysDictionaryDTO> info(
            @RequestBody @Validated KeyVO<Long> id) {
        log.debug(I18nMsgHelper.get("api.call.sysDictionary.info") + " == {}", id.getKey());
        return invokeMethod(sysDictionaryService::info, id);
    }

    /**
     * 查询字典配置信息ID-NAME列表
     *
     * @param paramType 要查询的字典类型
     * @return
     */
    @PostMapping("/id-name")
    @PermissionsFlag("sys:dictionary:id-name")
    public ApiResult<List<IdNameVO>> listIdName(
            @RequestBody @Validated KeyVO<String> paramType) {
        log.debug(I18nMsgHelper.get("api.call.sysDictionary.listIdName") + " == {}", paramType);
        return successResultWithData(sysDictionaryService.listIdName(paramType.getKey()));
    }

    /**
     * 查询字典信息列表
     *
     * @return
     */
    @PostMapping("/listByType")
    @PermissionsFlag("sys:dictionary:listByType")
    public ApiResult<List<SysDictionaryDTO>> listByType(
            @RequestBody @Validated KeyVO<String> paramType) {
        log.debug(I18nMsgHelper.get("api.call.sysDictionary.listByType") + " == {}", paramType);
        return successResultWithData(sysDictionaryService.listByParamType(paramType.getKey()));
    }

}
